package board;

import board.model.ReplyVO;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.ibatis.common.resources.Resources;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapClientBuilder;

import java.util.*;
import java.io.Reader;
import java.io.IOException;
import java.io.File;

import local.model.*;

import org.apache.commons.io.FileUtils;

public class writeReplyAction extends ActionSupport {
	
	public static Reader reader;
	public static SqlMapClient sqlMapper;
	
	private ReplyVO paramClass;
	private ReplyVO resultClass;
	
	private int Article_no;
	private int Rep_no;
	private String Content;
	private String Thanklist;
	private Date Regdate;
	Calendar today = Calendar.getInstance();
	
	private File upload;
	private String uploadContentType;
	private String uploadFileName;
	private String fileUploadPath = "C:\\javaWeb9\\upload\\";
	private String File_org;
	private String File_sav;
	
	public writeReplyAction() throws IOException {
		reader = Resource.getResourceAsReader("sqlMapConfig.xml");
		sqlMapper = SqlMapClientBuilder.buildSqlMapClient(reader);
		reader.close();
	}
	public String form() throws Exception {
		return SUCCESS;
	}
	public String execute() throws Exception {
		paramClass = new ReplyVO();
		resultClass = new ReplyVO();
		
		paramClass.setArticle_no(getArticle_no());
		paramClass.setContent(getContent());
		paramClass.setRegdate(today.getTime());
		paramClass.setThanklist("");
		
		paramClass.setWriter(ActionContext.getContext().getSession().get("Name"));
		paramClass.setLocal(ActionContext.getContext().getSession().get("Local"));
		paramClass.setSav_img(ActionContext.getContext().getSession().get("Sav_img"));
		
		sqlMapper.insert("insertReply", paramClass);
		
		if(getUpload() != null) {
			
			resultClass = (ReplyVO)sqlMapper.queryForObject("selectLastNo");
			
			String file_name = "file_" + resultClass.getRep_no();
			String file_ext = getUploadFileName().substring(
							getUploadFileName().lastIndexOf('.') + 1,
							getUploadFileName().length());
			
			File destFile = new File(fileUploadPath + file_name + "."
							+ file_ext);
			FileUtils.copyFile(getUpload(), destFile);
			
			paramClass.setRep_no(resultClass.getRep_no());
			paramClass.setFile_orgname(getUploadFileName());
			paramClass.setFile_savname(file_name + "." + file_ext);
			
			sqlMapper.update("updateFile", paramClass);
		}
		return SUCCESS;
	}
	
	public ReplyVO getParamClass() {
		return paramClass;
	}
	public void setParamClass(ReplyVO paramClass) {
		this.paramClass = paramClass;
	}
	public ReplyVO getResultClass() {
		return resultClass;
	}
	public void setResultClass(ReplyVO resultClass) {
		this.resultClass = resultClass;
	}
	public int getArticle_no() {
		return Article_no;
	}
	public void setArticle_no(int article_no) {
		Article_no = article_no;
	}
	public int getRep_no() {
		return Rep_no;
	}
	public void setRep_no(int rep_no) {
		Rep_no = rep_no;
	}
	public String getContent() {
		return Content;
	}
	public void setContent(String content) {
		Content = content;
	}
	public String getThanklist() {
		return Thanklist;
	}
	public void setThanklist(String thanklist) {
		Thanklist = thanklist;
	}
	public Date getRegdate() {
		return Regdate;
	}
	public void setRegdate(Date regdate) {
		Regdate = regdate;
	}
	public Calendar getToday() {
		return today;
	}
	public void setToday(Calendar today) {
		this.today = today;
	}
	public File getUpload() {
		return upload;
	}
	public void setUpload(File upload) {
		this.upload = upload;
	}
	public String getUploadContentType() {
		return uploadContentType;
	}
	public void setUploadContentType(String uploadContentType) {
		this.uploadContentType = uploadContentType;
	}
	public String getUploadFileName() {
		return uploadFileName;
	}
	public void setUploadFileName(String uploadFileName) {
		this.uploadFileName = uploadFileName;
	}
	public String getFileUploadPath() {
		return fileUploadPath;
	}
	public void setFileUploadPath(String fileUploadPath) {
		this.fileUploadPath = fileUploadPath;
	}
	public String getFile_org() {
		return File_org;
	}
	public void setFile_org(String File_org) {
		this.File_org = File_org;
	}
	public String getFile_sav() {
		return File_sav;
	}
	public void setFile_sav(String File_sav) {
		this.File_sav = File_sav;
	}
}
